package com.affaris.controller;


import com.affaris.common.QueryPageParam;
import com.affaris.common.Result;
import com.affaris.entity.Apply;
import com.affaris.entity.Trans;
import com.affaris.entity.Users;
import com.affaris.service.TransService;
import com.affaris.service.UsersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lucas
 * @since 2023-05-23
 */
@RestController
@RequestMapping("/trans")
public class TransController {

    @Autowired
    private TransService transService;
    @Autowired
    private UsersService usersService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Trans trans) {
        return transService.save(trans) ? Result.suc() : Result.fail();
    }

    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {

        return transService.removeById(id) ? Result.suc() : Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Trans trans) {

        boolean transUpdated = transService.updateById(trans);
        if (!transUpdated) {
            return Result.fail();
        }
        // 构建更新条件
        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("student_id", trans.getStudentId())
                .set("organization", trans.getTransfer());
        boolean usersUpdated = usersService.update(updateWrapper);

        return usersUpdated ? Result.suc() : Result.fail();

//        return transService.updateById(trans) ? Result.suc() : Result.fail();
    }

    //拒绝转入
    @PostMapping("/refuse")
    public Result refuse(@RequestBody Trans trans) {

        return transService.updateById(trans) ? Result.suc() : Result.fail();
    }

    //查询
    @PostMapping("/page")
    public Result page(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String transfer = (String) param.get("transfer");
        String original=(String)param.get("original");

        Page<Trans> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Trans> lambdaQueryWrapper = new LambdaQueryWrapper();

        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Trans::getName, name);
        }
        if (StringUtils.isNotBlank(transfer)&& !"null".equals(transfer)) {
            lambdaQueryWrapper.eq(Trans::getTransfer, transfer);
        }
        if (StringUtils.isNotBlank(original)&& !"null".equals(original)) {
            lambdaQueryWrapper.eq(Trans::getOriginal, original);
        }

        IPage result = transService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    //查询
    @PostMapping("/myPage")
    public Result myPage(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String studentId = (String) param.get("studentId");


        Page<Trans> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Trans> lambdaQueryWrapper = new LambdaQueryWrapper();

        if (StringUtils.isNotBlank(studentId) && !"null".equals(studentId)) {
            lambdaQueryWrapper.eq(Trans::getStudentId, studentId);
        }
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Trans::getName, name);
        }

        IPage result = transService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    //重复申请验证
    @PostMapping("/duplicate")
    public Result duplicate(@RequestBody Trans trans) {
        LambdaQueryWrapper<Trans> lambdaQueryWrapper = new LambdaQueryWrapper<Trans>()
                .eq(Trans::getStudentId, trans.getStudentId())
                .eq(Trans::getStatu,trans.getStatu());
        Trans result = transService.getOne(lambdaQueryWrapper);
        if (result != null) {
            return Result.fail();
        } else {
            return Result.suc();
        }
    }

}
